<?php
/**
 * TODO 
 *
 *
 */
class LoginController extends MasterController {

    /**
     * A lot of checks to see if cookie is set or not. Render views depending on if the user is logged in or no.
     * @return string $return 
     * 
     * TODO: Change variable-name $return
     * TODO: Abstract more to the view
     * TODO: Make an error-class for error handling
     */    
    public function doControll() {

        $return = "" ;
    
        if($this->view->triedToLogin()) {
            if($this->model->doLogin($this->view->getUsername(), $this->view->getPassword())) {
                if($this->view->isRememberMe()) {
                    if($this->view->setRememberMe()) {
                        $return .= $this->view->addNotice("Du kommer kommas ihåg nästa gång du loggar in.") ;
                    }
                }
            } else {
                $return .= $this->view->returnError(1) ;
            }
        }

        if($this->view->triedToLogout()) {
            $this->model->doLogout() ;
            $this->view->setUnrememberMe() ;
        }

        if($this->model->isLoggedIn()) {
            $return .= $this->view->addNotice("Du är inloggad") ;
            $return .= $this->view->doLogoutBox() ;
        } else {
            $return .= $this->view->addNotice("Du är inte inloggad") ;
            $return .= $this->view->doLoginBox() ;    
        }

        $return = $this->view->addDiv($return, array("class"=>"loginBox")) ;
         
        return $return ;


    }

}
